package com.chc.login.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chc.utils.ResultUtils;
import com.chc.utils.ResultVo;
import com.github.kevinsawicki.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/03/31/21:16
 * @Description:
 */
@RestController
@RequestMapping("/wxapi/weChat")
public class LoginController {

    @RequestMapping("/wxLogin")
    public ResultVo wxLogin(@RequestParam("code") String code){
        Map<String,String> map = new HashMap<>();
        map.put("appid","wxffaa905aa941ce2b");
        map.put("secret","187157041a71abe140d22a7912cb67ea");
        map.put("js_code", code);
        map.put("grant_type", "authorization_code");
        String response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session").form(map).body();
        System.out.println("Response was: " + response);
        JSONObject obj= JSON.parseObject(response);//将json字符串转换为json对
        System.out.println(obj);
        return ResultUtils.success("成功",obj);
    }
}
